package com.traffic.fuwu.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.traffic.fuwu.mybatis.pojo.Account;
import com.traffic.fuwu.mybatis.pojo.AppVersion;
import com.traffic.fuwu.mybatis.pojo.Carry;
import com.traffic.fuwu.mybatis.pojo.Company;
import com.traffic.fuwu.mybatis.pojo.Device;
import com.traffic.fuwu.mybatis.pojo.Employee;
import com.traffic.fuwu.mybatis.pojo.Line;
import com.traffic.fuwu.mybatis.pojo.VehAppCarry;
import com.traffic.fuwu.mybatis.pojo.VehAppDevice;
import com.traffic.fuwu.mybatis.pojo.VehAppOldDevice;
import com.traffic.fuwu.mybatis.pojo.WhiteList;
import com.traffic.fuwu.service.AccountService;
import com.traffic.fuwu.service.AppVersionService;
import com.traffic.fuwu.service.CarryService;
import com.traffic.fuwu.service.CompanyService;
import com.traffic.fuwu.service.DeviceService;
import com.traffic.fuwu.service.EmployeeService;
import com.traffic.fuwu.service.LastHeartService;
import com.traffic.fuwu.service.LineService;
import com.traffic.fuwu.service.VehAppCarryService;
import com.traffic.fuwu.service.VehAppDeviceService;
import com.traffic.fuwu.service.VehAppOldDeviceService;
import com.traffic.fuwu.service.WhiteListService;
import com.traffic.fuwu.util.MD5Util;
import com.traffic.fuwu.util.Page;
import com.traffic.fuwu.util.ToolUtil;
import com.traffic.fuwu.util.UtilDate;
import com.traffic.fuwu.vo.VoCarry;
import com.traffic.fuwu.vo.VoCompany;
import com.traffic.fuwu.vo.VoDevice;
import com.traffic.fuwu.vo.VoEmployee;
import com.traffic.fuwu.vo.VoLastHeart;
import com.traffic.fuwu.vo.VoLine;
import com.traffic.fuwu.vo.VoStatus;
import com.traffic.fuwu.vo.VoVehAppCarry;
import com.traffic.fuwu.vo.VoVehAppDevice;
import com.traffic.fuwu.vo.VoVersion;
import com.traffic.fuwu.vo.VoWhiteList;

/**
 * 
 * @author pikewei
 * @date 2016-12-6
 */
@Controller
@RequestMapping("/trafficback")
public class TrafficBackController {
	private static final Logger log = LoggerFactory.getLogger(TrafficBackController.class);
	private static final VoStatus voStatus = new VoStatus();
	@Autowired
	AccountService accountService;
	@Autowired
	AppVersionService appVersionService;
	@Autowired
	CarryService carryservice;
	@Autowired
	DeviceService deviceService;
	@Autowired
	LastHeartService lastHeartService;
	@Autowired
	EmployeeService employeeService;
	@Autowired
	LineService lineService;
	@Autowired
	CompanyService companyService;
	@Autowired
	WhiteListService whiteListService;
	@Autowired
	VehAppCarryService vehAppCarryService;
	@Autowired
	VehAppDeviceService vehAppDeviceService;
	@Autowired
	VehAppOldDeviceService vehAppOldDeviceService;

	/*
	 * 登录验证
	 */
	@RequestMapping(value = "/login")
	@ResponseBody
	public Map<String, Object> login(String accountName, String accountPassword) {
		Map<String, Object> map = new HashMap<>();
		if (accountName != null && accountPassword != null) {
			// 通过帐号和密码得到account
			Account account = accountService.login(accountName, MD5Util.GetMD5Code(accountPassword));
			// Account account = accountService.login("admin",
			// MD5Util.GetMD5Code("admin"));

			// 验证account非空
			if (account != null) {
				String[] head = new String[] { "登录成功" };
				map.put("head", head);
				map.put("line", account);
				map.put("appKey", account.getAppKey());
				voStatus.setCode(1);
				voStatus.setMessage("登陆成功");
				map.put("status", voStatus);
				return map;
			}
			String[] head = new String[] { "登录失败，用户名或密码错误" };
			map.put("head", head);
			voStatus.setCode(0);
			voStatus.setMessage("登陆失败，用户名或者密码错误");
			map.put("status", voStatus);
			return map;
		}
		String[] head = new String[] { "登录失败，用户名或密码错误" };
		map.put("head", head);
		voStatus.setCode(0);
		voStatus.setMessage("登录失败，用户名或者密码错误");
		map.put("status", voStatus);
		return map;
	}

	/*
	 * 修改用户信息
	 */
	@RequestMapping(value = "/updateAccount")
	@ResponseBody
	public Map<String, Object> updateAccount(String accountName, String accountPassword, String newAccountPassword) {
		Map<String, Object> map = new HashMap<>();

		Account account = accountService.login(accountName, MD5Util.GetMD5Code(accountPassword));
		// System.out.println(account);
		if (account != null) {
			account.setAccountPassword(MD5Util.GetMD5Code(newAccountPassword));
			accountService.updateAccount(account);
			String[] head = new String[] { "修改成功,新密码为" + newAccountPassword };
			map.put("head", head);
			map.put("line", account);
			voStatus.setCode(1);
			voStatus.setMessage("修改成功");
			map.put("status", voStatus);
			return map;
		}
		String[] head = new String[] { "修改失败" };
		map.put("head", head);
		voStatus.setCode(0);
		voStatus.setMessage("修改失败");
		map.put("status", voStatus);
		return map;

	}

	/*
	 * 机具查询
	 */
	@RequestMapping(value = "/deviceList")
	@ResponseBody
	public Map<String, Object> deviceList(Model model, HttpSession session,
			@RequestParam(value = "pageIndex", required = false) String pageIndex,
			@RequestParam(value = "vehNo", required = false) String vehNo,
			@RequestParam(value = "lineName", required = false) String lineName,
			@RequestParam(value = "deviceId", required = false) String deviceId, HttpServletRequest request,
			HttpServletResponse response, String appKey) {

		Map<String, Object> map = new HashMap<>();

		String pageSize = "10";
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));
		// 查询数量
		int count = deviceService.countDevice(lineName, vehNo, deviceId, appKey);
		page = ToolUtil.getPage(count, page);
		try {
			if (vehNo != null && vehNo != "") {
				vehNo = URLDecoder.decode(vehNo, "UTF-8");
			}
			if (lineName != null && lineName != "") {
				lineName = URLDecoder.decode(lineName, "UTF-8");
			}
			List<Map> list = deviceService.selectDeviceFy(lineName, vehNo, deviceId, appKey, page.startRow(),
					page.getPageSize());
			// System.out.println(list.get(0));
			List<VoDevice> listDevice = new ArrayList<>();
			if (list != null && list.size() != 0) {
				for (int i = 0; i < list.size(); i++) {
					VoDevice voDevice = new VoDevice();
					String a_carryCode = (String) list.get(i).get("carry_code");
					String b_deviceId = (String) list.get(i).get("device_id");
					// 数据库int有null值时后台用Integer接收
					Integer c_isChan1 = (Integer) list.get(i).get("is_chan");
					String ischan = c_isChan1 + "";// int转String
					if (ischan == "1") {
						String c_isChan = "正向";
						voDevice.setIsChan(c_isChan);
					} else if (ischan == "2") {
						String c_isChan = "反向";
						voDevice.setIsChan(c_isChan);
					} else {
						String c_isChan = "暂无数据";
						voDevice.setIsChan(c_isChan);
					}

					String d_lineName = (String) list.get(i).get("line_name");
					String e_lineShort = (String) list.get(i).get("line_short");
					String f_gprsCode = (String) list.get(i).get("gprs_card");

					int perAmount = (int) list.get(i).get("per_amount");
					Double sb = ((double) perAmount) / 100;
					String g_preMount = String.valueOf(sb);

					if (StringUtils.isEmpty(a_carryCode)) {
						voDevice.setCarryCode("暂无数据");
					} else {
						voDevice.setCarryCode(a_carryCode);
					}

					if (StringUtils.isEmpty(b_deviceId)) {
						voDevice.setDeviceId("暂无数据");
					} else {
						voDevice.setDeviceId(b_deviceId);
					}

					if (StringUtils.isEmpty(d_lineName)) {
						voDevice.setLineName("暂无数据");
					} else {
						voDevice.setLineName(d_lineName);
					}

					if (StringUtils.isEmpty(e_lineShort)) {
						voDevice.setLineShort("暂无数据");
					} else {
						voDevice.setLineShort(e_lineShort);
					}

					if (StringUtils.isEmpty(f_gprsCode)) {
						voDevice.setGprsCode("暂无数据");
					} else {
						voDevice.setGprsCode(f_gprsCode);
					}
					voDevice.setPreMount(g_preMount);
					listDevice.add(voDevice);
				}
				VoDevice head = new VoDevice();
				head.setDeviceId("设备号");
				head.setCarryCode("车牌号");
				head.setGprsCode("GPRS编号");
				head.setIsChan("正反向");
				head.setPreMount("单笔金额（元）");
				head.setLineName("线路名");
				head.setLineShort("线路简称");
				/*
				 * String[] head = new String[] { "车牌号", "设备号", "正反向", "线路名",
				 * "线路简称", "GPRS编号", "单笔金额（元）" };
				 */
				map.put("head", head);
				map.put("line", listDevice);
				map.put("page", page);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			map.put("head", "没有查询到相关数据");
			voStatus.setCode(0);
			voStatus.setMessage("没有查询到相关数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("接口异常");
			voStatus.setCode(3);
			voStatus.setMessage("接口异常");
			map.put("status", voStatus);
			return map;
		}

	}

	/*
	 * 根据机具编号得到机具对象
	 */
	@RequestMapping(value = "/getDevice")
	@ResponseBody
	public Map<String, Object> getDevice(String deviceId) {
		Map<String, Object> map = new HashMap<>();
		try {
			do {
				if (StringUtils.isAnyBlank(deviceId)) {
					voStatus.setCode(2);
					voStatus.setMessage("查询失败，请输入车牌号");
					map.put("status", voStatus);
					break;
				}
				Device device = deviceService.getDevice(deviceId);
				if (null == device) {
					voStatus.setCode(0);
					voStatus.setMessage("查询失败");
					map.put("status", voStatus);
					break;
				}
				if (device.getGprsCard() == null) {
					device.setGprsCard("暂无数据");
				}
				map.put("line", device);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
			} while (false);

		} catch (Exception e) {
			e.printStackTrace();
			log.info("接口异常");
			voStatus.setCode(4);
			voStatus.setMessage("接口异常");
			map.put("status", voStatus);
		}
		return map;
	}

	/*
	 * 机具信息修改
	 */
	@RequestMapping(value = "/updateDevice")
	@ResponseBody
	public Map<String, Object> updatedevice(String deviceId, String carryCode, String lineName, String lineShort,
			String isChan, String perAmount, String newDeviceId, String gprsCard) {
		Map<String, Object> map = new HashMap<>();
		try {
			Device device = deviceService.getDevice(deviceId);

			if (device != null) {
				if (carryCode != null) {
					device.setCarryCode(carryCode);
				}
				if (lineName != null) {
					lineName = URLDecoder.decode(lineName, "UTF-8");
					device.setLineName(lineName);
				}
				if (gprsCard != null) {
					device.setGprsCard(gprsCard);
				}
				if (lineShort != null) {
					device.setLineShort(lineShort);
					device.setLineCode(lineShort);
				}
				if (newDeviceId != null) {
					device.setDeviceId(newDeviceId);
				}
				if (isChan.equals("正向")) {
					int isChans = 1;
					device.setIsChan(isChans);
				} else if (isChan.equals("反向")) {
					int isChans = 2;
					device.setIsChan(isChans);
				}
				if (perAmount != null) {
					double amountindex = Double.parseDouble(perAmount) * 100;
					String amountsecount = StringUtils.split(String.valueOf(amountindex), ".")[0];
					device.setPerAmount(Integer.parseInt(amountsecount));
				}
				device.setUpdateTime(new Date());
				deviceService.updateDevice(device);
				voStatus.setCode(1);
				voStatus.setMessage("修改成功");
				map.put("status", voStatus);
				map.put("line", device);
				return map;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		voStatus.setCode(0);
		voStatus.setMessage("修改失败");
		map.put("status", voStatus);
		return map;
	}

	/*
	 * 增加新机具
	 */

	@RequestMapping(value = "/addDevice")
	@ResponseBody
	public Map<String, Object> addDevice(String deviceId, String carryCode, String lineName, String lineShort,
			String isChan, String perAmount, String appKey, String gprsCard) {

		Map<String, Object> map = new HashMap<>();

		if (appKey != null && appKey != "") {
			Device device = new Device();
			Device oldDevice = deviceService.getDevice(deviceId);
			if (oldDevice != null) {
				String[] head = new String[] { "增加失败，机具编号或者牌照号已存在" };
				map.put("head", head);
				return map;
			}
			double amountindex = Double.parseDouble(perAmount) * 100;
			String amountsecount = StringUtils.split(String.valueOf(amountindex), ".")[0];
			Integer ischan = 0;
			if (isChan == "正向") {
				ischan = 1;
			} else {
				ischan = 2;
			}

			device.setDeviceId(deviceId);
			device.setLineName(lineName);
			device.setCarryCode(carryCode);
			device.setPerAmount(Integer.parseInt(amountsecount));
			device.setLineShort(lineShort);
			device.setAppKey(appKey);
			device.setCreateTime(new Date());
			device.setGprsCard(gprsCard);
			device.setIsChan(ischan);
			deviceService.addDevice(device);
			String[] head = new String[] { "增加成功" };
			map.put("head", head);
			map.put("line", device);
			return map;
		}

		String[] head = new String[] { "无效appKey" };
		map.put("head", head);
		return map;

	}

	/*
	 * 查询心跳
	 */
	@RequestMapping(value = "/selectHeart")
	@ResponseBody
	public Map<String, Object> selectHeart(Model model,
			@RequestParam(value = "pageIndex", required = false) String pageIndex,
			@RequestParam(value = "deviceId", required = false) String deviceId,
			@RequestParam(value = "locate", required = false) String locate,
			@RequestParam(value = "vehNo", required = false) String vehNo,
			@RequestParam(value = "createTime", required = false) Date createTime, String appKey) {
		Map<String, Object> map = new HashMap<>();

		String pageSize = "10";// 每页显示的行数
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));
		int count = lastHeartService.countHeart(deviceId, locate, vehNo, appKey, createTime);// 查询数量
		page = ToolUtil.getPage(count, page);

		try {
			if (vehNo != null && vehNo != "") {
				vehNo = URLDecoder.decode(vehNo, "UTF-8");
			}
			List<Map> list = lastHeartService.selectHeartFyz(deviceId, locate, vehNo, appKey, createTime,
					page.startRow(), page.getPageSize());
			if (list != null && list.size() != 0) {

				List<VoLastHeart> listHeart = new ArrayList<>();

				for (int i = 0; i < list.size(); i++) {
					VoLastHeart voLastHeart = new VoLastHeart();
					String carryCode = (String) list.get(i).get("veh_no");
					String deviceId1 = (String) list.get(i).get("device_id");
					Date createTime1 = (Date) list.get(i).get("create_time");
					int locate1 = (int) list.get(i).get("locate");
					if (locate1 == 0) {
						String locate2 = "正常";
						voLastHeart.setLocate(locate2);
					} else {
						String locate2 = "出错";
						voLastHeart.setLocate(locate2);
					}
					voLastHeart.setCarryCode(carryCode);
					voLastHeart.setDeviceId(deviceId1);
					voLastHeart.setCreateTime(createTime1);
					listHeart.add(voLastHeart);
				}

				VoLastHeart head = new VoLastHeart();
				head.setCarryCode("车牌号");
				head.setDeviceId("机具编号");
				head.setLocate("实时状态");
				map.put("head", head);
				map.put("line", listHeart);
				map.put("page", page);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			map.put("head", "没有查询到相关数据");
			voStatus.setCode(0);
			voStatus.setMessage("没有查到相关数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("获取失败");
			voStatus.setCode(2);
			voStatus.setMessage("接口异常");
			map.put("status", voStatus);
			return map;
		}
	}

	/*
	 * 查询版本信息
	 */
	@RequestMapping(value = "/selectVersion")
	@ResponseBody
	public Map<String, Object> selectVersion(Model model,
			@RequestParam(value = "pageIndex", required = false) String pageIndex,
			@RequestParam(value = "version", required = false) String version, HttpSession session,
			HttpServletRequest request, HttpServletResponse response, String appKey) {

		Map<String, Object> map = new HashMap<>();

		if (appKey != null && appKey != "") {

			String pageSize = "10";// 每一页的行数
			if (StringUtils.isEmpty(pageIndex)) {
				pageIndex = "1";
			}
			Page page = new Page();
			page.setPageIndex(Integer.parseInt(pageIndex));
			page.setPageSize(Integer.parseInt(pageSize));

			int count = appVersionService.newconntVersion(version, appKey);

			page = ToolUtil.getPage(count, page);
			try {
				if (version != null && version != "") {
					version = URLDecoder.decode(version, "UTF-8");
				}
				List<Map> list = appVersionService.selectVersionFy(version, appKey, page.startRow(),
						page.getPageSize());
				List<VoVersion> versionList = new ArrayList<>();
				if (list != null && list.size() != 0) {
					for (int i = 0; i < list.size(); i++) {
						VoVersion voVersion = new VoVersion();
						String a_version = (String) list.get(i).get("version");
						String b_packagePath = (String) list.get(i).get("package_path");
						String appKeyTemp = (String) list.get(i).get("app_key");
						Date c_createTime = (Date) list.get(i).get("create_time");

						voVersion.setAppKey(appKeyTemp);
						voVersion.setVersion(a_version);
						voVersion.setPackagePath(b_packagePath);
						voVersion.setCreateTime(c_createTime);

						versionList.add(voVersion);
					}

					// String[] head = new String[] { "版本号", "升级文件名", "上传时间" };
					VoVersion head = new VoVersion();
					head.setVersion("版本号");
					head.setAppKey("企业唯一标识");
					head.setPackagePath("升级文件名");
					map.put("head", head);
					map.put("line", versionList);
					map.put("page", page);
					voStatus.setCode(1);
					voStatus.setMessage("查询成功");
					map.put("status", voStatus);
					return map;
				} else {
					map.put("head", "没有查到相关数据");
					voStatus.setCode(0);
					voStatus.setMessage("没有查到相关数据");
					map.put("status", voStatus);
					return map;
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.info("获取失败！");

			}
		}
		map.put("head", "无效appKey");
		return map;
	}

	/*
	 * 新版本的上传
	 */
	@RequestMapping(value = "/addVersion")
	@ResponseBody
	public Map<String, Object> addVersion(@RequestParam(value = "file", required = false) CommonsMultipartFile file,
			HttpServletRequest request, String version, HttpSession session, HttpServletResponse response,
			String appKey) {

		Map<String, Object> map = new HashMap<>();

		// MultipartFile是对当前上传的文件的封装，当要同时上传多个文件时，可以给定多个MultipartFile参数(数组)
		/*if (appKey != null && appKey != "") {
			if (file != null) {
				String type = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));// 取文件格式后缀名
				String filename = System.currentTimeMillis() + type;// 取当前时间戳作为文件名
				String path = request.getSession().getServletContext().getRealPath("/upload/" + filename);// 存放位置
				// String
				// path="/disk1/tomcat/tomcat-8080-qrcodebus/webapps/ROOT/file";
				File destFile = new File(path);
				try {
					// FileUtils.copyInputStreamToFile()这个方法里对IO进行了自动操作，不需要额外的再去关闭IO流
					FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);// 复制临时文件到指定目录下
				} catch (IOException e) {
					e.printStackTrace();
				}
				AppVersion appVersion = new AppVersion();
				appVersion.setVersionDesc(version + "号版本");
				appVersion.setPackagePath(filename);
				appVersion.setAppKey(appKey);
				appVersion.setCreateTime(new Date());
				appVersionService.addVersion(appVersion);
				// System.out.println(type);
				// System.out.println(filename);
				// System.out.println(path);
				// System.out.println(destFile);
				String[] head = new String[] { "上传成功" };
				map.put("head", head);
				map.put("line", version);
				return map;
			} else {
				String[] head = new String[] { "上传失败,上传文件不能为空" };
				map.put("head", head);
				return map;
			}
		}
		String[] head = new String[] { "无效appkey" };
		map.put("head", head);*/
		return map;
	}

	/*
	 * 根据车牌号来查询车辆信息
	 */
	@RequestMapping(value = "/getCarry")
	@ResponseBody
	public Map<String, Object> getCarry(String carryCode, String appKey) {
		Map<String, Object> map = new HashMap<>();
		if (StringUtils.isNotBlank(carryCode)) {
			try {
				carryCode = URLDecoder.decode(carryCode, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		if (carryCode != null && appKey != null) {
			Carry carry = carryservice.getCarry(carryCode, appKey);
			if (carry != null) {
				String[] head = new String[] { "已查到该车辆信息" };
				map.put("head", head);
				map.put("lineName", carry.getLineName());
				map.put("unitPrice", carry.getUnitPrice());
				map.put("lineCode", carry.getLineCode());
				return map;
			} else {
				String[] head = new String[] { "您输入的牌照号有误" };
				map.put("head", head);
				return map;
			}
		}
		String[] head = new String[] { "请输入车牌号，或者无效appKey" };
		map.put("head", head);
		return map;
	}

	/*
	 * 判断机具号的唯一性
	 */
	@RequestMapping(value = "/onlyDeviceId")
	@ResponseBody
	public Map<String, Object> onlyDeviceId(String deviceId) {
		Map<String, Object> map = new HashMap<>();
		if (deviceId != null) {
			Device device = deviceService.getDevice(deviceId);
			if (device != null) {
				String[] head = new String[] { "该机具号已存在，请重新输入" };
				map.put("head", head);
				return map;
			} else {
				String[] head = new String[] { "该机具号可以使用" };
				map.put("head", head);
				return map;
			}
		}
		String[] head = new String[] { "请输入机具号" };
		map.put("head", head);
		return map;
	}

	/*
	 * 判断车牌号的唯一性
	 */
	@RequestMapping(value = "/onlyCarryCode")
	@ResponseBody
	public Map<String, Object> onlyCarryCode(String carryCode) {
		Map<String, Object> map = new HashMap<>();
		if (carryCode != null) {
			Device device = deviceService.getDevicecc(carryCode);
			if (device != null) {
				String[] head = new String[] { "该机具号已存在，请重新输入" };
				map.put("head", head);
				return map;
			} else {
				String[] head = new String[] { "该机具号可以使用" };
				map.put("head", head);
				return map;
			}
		}
		String[] head = new String[] { "请输入车牌号" };
		map.put("head", head);
		return map;
	}

	/*
	 * 查询载具信息
	 */
	@RequestMapping(value = "/carryList")
	@ResponseBody
	public Map<String, Object> crryList(HttpSession session,
			@RequestParam(value = "pageIndex", required = false) String pageIndex,
			@RequestParam(value = "carryCode", required = false) String carryCode,
			@RequestParam(value = "lineCode", required = false) String lineCode,
			@RequestParam(value = "lineName", required = false) String lineName,
			@RequestParam(value = "deptName", required = false) String deptName, Model model, String appKey) {
		Map<String, Object> map = new HashMap<>();

		String pageSize = "10";// 每一页的行数
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));

		try {
			if (carryCode != null && carryCode != "") {
				carryCode = URLDecoder.decode(carryCode, "UTF-8");
			}
			if (lineCode != null && lineCode != "") {
				lineCode = URLDecoder.decode(lineCode, "UTF-8");
			}
			if (lineName != null && lineName != "") {
				lineName = URLDecoder.decode(lineName, "UTF-8");
			}
			if (deptName != null && deptName != "") {
				deptName = URLDecoder.decode(deptName, "UTF-8");
			}
			// 分页总数量
			int count = carryservice.getCarryCount(carryCode, lineCode, lineName, deptName, appKey);
			page = ToolUtil.getPage(count, page);
			// 初始化VoCarry
			List<VoCarry> voCarryList = new ArrayList<>();
			// 分页查询
			List<Carry> list = carryservice.getCarryList(carryCode, lineCode, lineName, deptName, appKey,
					page.startRow(), page.getPageSize());
			if (list != null && list.size() != 0) {
				for (Carry carry : list) {
					VoCarry voCarry = new VoCarry();
					double amount = carry.getUnitPrice();
					String unitPrice = String.valueOf(amount / 100);
					voCarry.setCarryCode(carry.getCarryCode());
					voCarry.setCarryName(carry.getCarryNo());
					voCarry.setDeptCode(carry.getDeptCode());
					voCarry.setDeptName(carry.getDeptName());
					voCarry.setCompanyName(carry.getCompanyName());
					voCarry.setIsProducing(carry.getIsProducing());
					voCarry.setLineCode(carry.getLineCode());
					voCarry.setLineName(carry.getLineName());
					voCarry.setUnitPrice(unitPrice);
					voCarryList.add(voCarry);
				}

				VoCarry head = new VoCarry();
				head.setCarryCode("车编号");
				head.setCarryName("自编号");
				head.setDeptCode("部门编号");
				head.setDeptName("部门名称");
				head.setCompanyName("公司名称");
				head.setIsProducing("是否运营");
				head.setLineCode("线路编号");
				head.setLineName("线路名称");
				head.setUnitPrice("票价（元）");

				map.put("head", head);
				map.put("line", list);
				map.put("page", page);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("查无数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	/*
	 * 员工信息查询
	 */
	@RequestMapping(value = "/employeeList")
	@ResponseBody
	public Map<String, Object> employeeList(HttpSession session,
			@RequestParam(value = "pageIndex", required = false) String pageIndex,
			@RequestParam(value = "empCode", required = false) String empCode,
			@RequestParam(value = "empName", required = false) String empName,
			@RequestParam(value = "IDcard", required = false) String IDcard,
			@RequestParam(value = "deptName", required = false) String deptName, Model model, String appKey) {
		Map<String, Object> map = new HashMap<>();

		String pageSize = "10";// 每一页的行数
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));
		try {
			if (empName != null && empName != "") {
				empName = URLDecoder.decode(empName, "UTF-8");
			}
			if (empCode != null && empCode != "") {
				empCode = URLDecoder.decode(empCode, "UTF-8");
			}
			if (deptName != null && deptName != "") {
				deptName = URLDecoder.decode(deptName, "UTF-8");
			}
			// 分页总数量
			int count = employeeService.getEmployeeCount(empCode, empName, IDcard, deptName, appKey);
			page = ToolUtil.getPage(count, page);
			// 分页查询
			List<Employee> list = employeeService.getEmployeeList(empCode, empName, IDcard, deptName, appKey,
					page.startRow(), page.getPageSize());
			if (list != null && list.size() != 0) {
				// 初始化VoEmployee
				List<VoEmployee> voEmployeeList = new ArrayList<>();
				for (Employee employee : list) {
					VoEmployee voEmployee = new VoEmployee();
					voEmployee.setEmpCode(employee.getEmpCode());
					voEmployee.setEmpName(employee.getEmpName());
					voEmployee.setGender(employee.getGender());
					voEmployee.setTelephone(employee.getTelephone());
					voEmployee.setCompanyName(employee.getCompanyName());
					voEmployeeList.add(voEmployee);
				}

				VoEmployee head = new VoEmployee();
				head.setEmpCode("员工编号");
				head.setEmpName("员工姓名");
				head.setGender("员工性别");
				head.setTelephone("员工联系方式");
				head.setCompanyName("公司名称");
				map.put("page", page);
				map.put("head", head);
				map.put("line", list);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("查无数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	/*
	 * 线路信息查询
	 */
	@RequestMapping(value = "/lineList")
	@ResponseBody
	public Map<String, Object> lineList(HttpSession session,
			@RequestParam(value = "pageIndex", required = false) String pageIndex,
			@RequestParam(value = "lineCode", required = false) String lineCode,
			@RequestParam(value = "lineName", required = false) String lineName, Model model, String appKey) {

		Map<String, Object> map = new HashMap<>();

		String pageSize = "10";// 每一页的行数
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));

		try {
			if (lineName != null && lineName != "") {
				lineName = URLDecoder.decode(lineName, "UTF-8");
			}
			if (lineCode != null && lineCode != "") {
				lineCode = URLDecoder.decode(lineCode, "UTF-8");
			}
			// 分页总数量
			int count = lineService.getLineCount(lineCode, lineName, appKey);
			page = ToolUtil.getPage(count, page);
			// 分页查询
			List<Line> list = lineService.getLineList(lineCode, lineName, appKey, page.startRow(), page.getPageSize());
			List<VoLine> voLineList = new ArrayList<>();
			if (list != null && list.size() != 0) {
				for (Line line : list) {
					VoLine voLine = new VoLine();
					voLine.setCategory(line.getCategory());
					voLine.setCircle(line.getCircle());
					voLine.setCompanyName(line.getCompanyName());
					voLine.setDeptCode(line.getDeptCode());
					voLine.setDeptName(line.getDeptName());
					voLine.setGprsCode(line.getGpsCode());
					voLine.setLineCode(line.getLineCode());
					voLine.setLineName(line.getLineName());
					voLine.setLineShort(line.getLineShort());
					voLine.setLineCount(line.getLineCount());
					voLineList.add(voLine);
				}

				VoLine head = new VoLine();
				head.setCategory("线路分类");
				head.setCircle("是否循环线路");
				head.setCompanyName("公司名称");
				head.setDeptCode("部门编号");
				head.setDeptName("部门名称");
				head.setGprsCode("GPS车载机上的对应编号");
				head.setLineCode("线路编号");
				head.setLineName("线路名称");
				head.setLineShort("线路短号");
				head.setLineCount("配车/船数");
				map.put("page", page);
				map.put("head", head);
				map.put("line", voLineList);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("查无数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	/*
	 * 公司查询
	 */
	@RequestMapping(value = "/companyList")
	@ResponseBody
	public Map<String, Object> companyList(String comName, String comAddress, String appKey, String pageIndex) {
		Map<String, Object> map = new HashMap<>();

		String pageSize = "10";// 每一页的行数
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));
		try {
			if (comName != null && comName != "") {
				comName = URLDecoder.decode(comName, "UTF-8");
			}
			if (comAddress != null && comAddress != "") {
				comAddress = URLDecoder.decode(comAddress, "UTF-8");
			}
			// 分页总数量
			int count = companyService.getFindCompanyCount(comName, comAddress, appKey);
			page = ToolUtil.getPage(count, page);

			List<Company> list = companyService.getFindCompany(comName, comAddress, appKey, page.startRow(),
					page.getPageSize());
			List<VoCompany> listCompany = new ArrayList<>();
			if (list != null && list.size() != 0) {
				for (Company company : list) {
					VoCompany voCompany = new VoCompany();
					voCompany.setComAddress(company.getComAddress());
					voCompany.setComCode(company.getComCode());
					voCompany.setComName(company.getComName());
					voCompany.setComParentCode(company.getComParentCode());
					listCompany.add(voCompany);
				}
				VoCompany head = new VoCompany();
				head.setComAddress("公司地址");
				head.setComCode("公司编号");
				head.setComName("公司名称");
				head.setComParentCode("企业编号");
				map.put("head", head);
				map.put("line", listCompany);
				map.put("page", page);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;

			}
			voStatus.setCode(0);
			voStatus.setMessage("查无数据");
			map.put("status", voStatus);
			return map;

		} catch (Exception e) {
			e.printStackTrace();

		}
		return map;
	}

	/*
	 * 白名单查询
	 */
	@RequestMapping(value = "/whiteList")
	@ResponseBody
	public Map<String, Object> whiteList(HttpServletRequest request, String userId, String userName, String appKey,
			String pageIndex) {
		Map<String, Object> map = new HashMap<>();
		String pageSize = "10";// 每一页的行数
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		String status = request.getParameter("status");
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));
		try {
			if (StringUtils.isNotBlank(userName)) {
				userName = URLDecoder.decode(userName, "UTF-8");
			}
			Map<String, Object> conditions = new HashMap<>();
			conditions.put("userId", userId);
			conditions.put("userName", userName);
			if (null != status && "" != status) {
				conditions.put("status", Integer.valueOf(status));
			}
			conditions.put("appKey", appKey);
			int count = whiteListService.getFindWhiteListCount(conditions);
			page = ToolUtil.getPage(count, page);

			conditions.put("pageSize", page.getPageSize());
			conditions.put("startRow", page.startRow());

			List<WhiteList> list = whiteListService.getFindWhiteList(conditions);
			List<VoWhiteList> voWhiteListList = new ArrayList<>();
			if (list != null && list.size() != 0) {
				for (WhiteList whiteList : list) {
					VoWhiteList voWhiteList = new VoWhiteList();
					voWhiteList.setWlId(whiteList.getWlId());
					voWhiteList.setUserId(whiteList.getUserId());
					voWhiteList.setUserName(whiteList.getUserName());
					// 金额转值
					double amount = whiteList.getPrice();
					String price = String.valueOf(amount / 100);
					voWhiteList.setPrice(price);
					voWhiteList.setStatus(whiteList.getStatus());
					Date date = whiteList.getCreateTime();
					voWhiteList.setCreateTime(UtilDate.getDateFormatter(date));
					voWhiteListList.add(voWhiteList);
				}
				VoWhiteList head = new VoWhiteList();
				head.setUserId("支付宝ID");
				head.setUserName("用户名");
				// head.setStatus("状态");
				head.setCreateTime("添加时间");
				map.put("head", head);
				map.put("line", voWhiteListList);
				map.put("page", page);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("查无数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	/*
	 * 根据id来获取whiteList对象
	 */
	@RequestMapping(value = "/getWhiteList")
	@ResponseBody
	public Map<String, Object> getWhitelList(int wlId) {
		Map<String, Object> map = new HashMap<>();
		try {
			WhiteList whiteList = whiteListService.getById(wlId);
			if (whiteList != null) {
				map.put("line", whiteList);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("查无数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/*
	 * 增加白名单
	 */
	@RequestMapping(value = "/addWhiteList")
	@ResponseBody
	public Map<String, Object> addWhiteList(String userId, String userName, String appKey, String price) {
		Map<String, Object> map = new HashMap<>();
		if (userId != null && userId != "" && userName != null && userName != "" && price != null && price != "") {
			try {
				WhiteList whtieList = new WhiteList();
				whtieList.setUserId(userId);
				whtieList.setUserName(userName);
				whtieList.setStatus(0);
				whtieList.setAppKey(appKey);
				double amountindex = Double.parseDouble(price) * 100;
				String amountsecount = StringUtils.split(String.valueOf(amountindex), ".")[0];
				whtieList.setPrice(Integer.parseInt(amountsecount));
				whtieList.setCreateTime(new Date());
				whiteListService.addWhiteList(whtieList);
				voStatus.setCode(1);
				voStatus.setMessage("增加成功");
				map.put("status", voStatus);
				return map;
			} catch (Exception e) {
				e.printStackTrace();
				voStatus.setCode(2);
				voStatus.setMessage("增加失败,接口异常");
				map.put("status", voStatus);
				return map;
			}
		}
		voStatus.setCode(0);
		voStatus.setMessage("增加失败");
		map.put("status", voStatus);
		return map;
	}

	/*
	 * 修改白名单信息
	 */
	@RequestMapping(value = "/updateWhiteList")
	@ResponseBody
	public Map<String, Object> updateWhiteList(int wlId, String status, String userId, String userName, String price) {
		Map<String, Object> map = new HashMap<>();
		try {
			WhiteList whiteList = whiteListService.getById(wlId);
			if (whiteList != null) {
				if (StringUtils.isNotBlank(userId)) {
					whiteList.setUserId(userId);
				}
				if (StringUtils.isNoneBlank(userName)) {
					userName = URLDecoder.decode(userName,"UTF-8");
					whiteList.setUserName(userName);
				}
				if (StringUtils.isNoneBlank(price)) {
					double amountindex = Double.parseDouble(price) * 100;
					whiteList.setPrice((int) amountindex);
				}
				whiteListService.updateWhiteList(whiteList);

			}
			voStatus.setCode(0);
			voStatus.setMessage("修改失败");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;

	}

	/*
	 * APP未录入车辆查询
	 */
	@RequestMapping(value = "/appListOuter")
	@ResponseBody
	public Map<String, Object> appListOuter(String regCode, String lineName, String code, String appKey,
			String pageIndex) {
		Map<String, Object> map = new HashMap<>();
		String pageSize = "10";// 每一页的行数
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));

		try {
			if (StringUtils.isNotBlank(regCode)) {
				regCode = URLDecoder.decode(regCode, "UTF-8");
			}

			Map<String, Object> conditions = new HashMap<>();
			conditions.put("vehNo", regCode);
			conditions.put("lineName", lineName);
			conditions.put("code", code);
			conditions.put("anarchy", 1);
			conditions.put("appKey", appKey);

			int count = vehAppCarryService.getAppCarryCount(conditions);
			page = ToolUtil.getPage(count, page);

			conditions.put("pageSize", page.getPageSize());
			conditions.put("startRow", page.startRow());

			List<VehAppCarry> list = vehAppCarryService.getAppCarry(conditions);
			if (list != null && list.size() != 0) {
				List<VoVehAppCarry> voVehAppCarryList = new ArrayList<>();
				for (VehAppCarry vehAppCarry : list) {
					VoVehAppCarry voVehAppCarry = new VoVehAppCarry();
					voVehAppCarry.setCode(vehAppCarry.getCode());
					voVehAppCarry.setLineName(vehAppCarry.getLineName());
					voVehAppCarry.setRegCode(vehAppCarry.getRegCode());
					voVehAppCarry.setDataid(vehAppCarry.getDataid());
					voVehAppCarry.setCreateTime(UtilDate.getDateFormatter(vehAppCarry.getCreateTime()));
					voVehAppCarryList.add(voVehAppCarry);
				}
				VoVehAppCarry head = new VoVehAppCarry();
				head.setCode("自编号");
				head.setCreateTime("添加世间");
				head.setLineName("线路名");
				head.setRegCode("车牌号");
				map.put("head", head);
				map.put("line", voVehAppCarryList);
				map.put("page", page);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("暂无数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	/*
	 * 根据Id查询vehAppCarry
	 */
	@RequestMapping(value = "/getByIdVehAppCarry")
	@ResponseBody
	public Map<String, Object> getByIdVehAppCarry(int dataid) {
		Map<String, Object> map = new HashMap<>();
		try {
			VehAppCarry vehAppCarry = vehAppCarryService.getById(dataid);
			if (vehAppCarry != null) {
				VoVehAppCarry line = new VoVehAppCarry();
				line.setCode(vehAppCarry.getCode());
				line.setCreateTime(UtilDate.getDateFormatter(vehAppCarry.getCreateTime()));
				line.setLineName(vehAppCarry.getLineName());
				line.setRegCode(vehAppCarry.getRegCode());
				map.put("line", line);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("查询失败");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/*
	 * 增加未安装车辆信息
	 */
	@RequestMapping(value = "/addVehAppCarry")
	@ResponseBody
	public Map<String, Object> addVehAppCarry(String regCode, String lineName, String code, String appKey) {
		Map<String, Object> map = new HashMap<>();
		if (regCode != null && regCode != "" && lineName != null && lineName != "" && code != null && code != ""
				&& appKey != null && appKey != "") {
			try {
				regCode = URLDecoder.decode(regCode, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			VehAppCarry vehAppCarry = new VehAppCarry();
			vehAppCarry.setRegCode(regCode);
			vehAppCarry.setLineName(lineName);
			vehAppCarry.setCode(code);
			vehAppCarry.setStatus(1);
			vehAppCarry.setCreateTime(new Date());
			vehAppCarry.setAppKey(appKey);
			vehAppCarryService.addVehAppCarry(vehAppCarry);
			voStatus.setCode(1);
			voStatus.setMessage("添加成功");
			map.put("status", voStatus);
			return map;
		}
		voStatus.setCode(0);
		voStatus.setMessage("添加失败");
		map.put("status", voStatus);
		return map;

	}

	/*
	 * 修改未安装App车辆信息(删除是直接赋值status为3)
	 */
	@RequestMapping(value = "/updateVehAppCarry")
	@ResponseBody
	public Map<String, Object> updateVehAppCarry(int dataid, String regCode, String lineName, String code,
			String status) {
		Map<String, Object> map = new HashMap<>();
		try {
			VehAppCarry vehAppCarry = vehAppCarryService.getById(dataid);
			if (vehAppCarry != null) {
				if (StringUtils.isNotBlank(regCode)) {
					regCode = URLDecoder.decode(regCode, "UTF-8");
					vehAppCarry.setRegCode(regCode);
				}
				if (StringUtils.isNotBlank(lineName)) {
					vehAppCarry.setLineName(lineName);
				}
				if (StringUtils.isNotBlank(code)) {
					vehAppCarry.setCode(code);
				}
				if (StringUtils.isNotBlank(status)) {
					vehAppCarry.setStatus(Integer.parseInt(status));
				}
				vehAppCarryService.updateVehAppCarry(vehAppCarry);
				voStatus.setCode(1);
				voStatus.setMessage("操作成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(1);
			voStatus.setMessage("操作失败");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/*
	 * 机具录入APP已录入查询
	 */
	@RequestMapping(value = "/appListInner")
	@ResponseBody
	public Map<String, Object> appListInter(String telephone, String deviceId, String vehNo, String address,
			String appKey, String pageIndex,String vehCode) {
		Map<String, Object> map = new HashMap<>();
		String pageSize = "10";// 每一页的行数
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));

		try {
			if (StringUtils.isNotBlank(vehNo)) {
				vehNo = URLDecoder.decode(vehNo, "UTF-8");
			}
			if (StringUtils.isNotBlank(address)) {
				address = URLDecoder.decode(address, "UTF-8");
			}

			Map<String, Object> conditions = new HashMap<>();
			conditions.put("vehCode", vehCode);
			conditions.put("telephone", telephone);
			conditions.put("deviceId", deviceId);
			conditions.put("vehNo", vehNo);
			conditions.put("address", address);
			conditions.put("appKey", appKey);

			int count = vehAppDeviceService.getFindVehAppDeviceCount(conditions);
			page = ToolUtil.getPage(count, page);

			conditions.put("pageSize", page.getPageSize());
			conditions.put("startRow", page.startRow());

			List<VehAppDevice> list = vehAppDeviceService.getFindVehAppDevice(conditions);
			if (list != null && list.size() != 0) {
				List<VoVehAppDevice> voVehAppDeviceList = new ArrayList<>();
				for (VehAppDevice vehAppDevice : list) {
					VoVehAppDevice voVehAppDevice = new VoVehAppDevice();
					voVehAppDevice.setTelephone(vehAppDevice.getTelephone());
					voVehAppDevice.setId(vehAppDevice.getId());
					voVehAppDevice.setAddress(vehAppDevice.getAddress());
					voVehAppDevice.setDeviceId(vehAppDevice.getDeviceId());
					voVehAppDevice.setNoteDetails(vehAppDevice.getNoteDetails());
					voVehAppDevice.setVehNo(vehAppDevice.getVehNo());
					voVehAppDevice.setVehCode(vehAppDevice.getVehCode());
					voVehAppDevice.setCreateTime(UtilDate.getDateFormatter(vehAppDevice.getCreateTime()));
					Integer note = vehAppDevice.getNote();
					String ischan = note + "";
					String notes = null;
					if (ischan.equals("1")) {
						notes = "换机具";
					} else if (ischan.equals("2")) {
						notes = "换流量卡";
					} else if (ischan.equals("3")) {
						notes = "其他";
					} else {
						notes = "暂无数据";
					}
					voVehAppDevice.setNote(notes);
					voVehAppDeviceList.add(voVehAppDevice);
				}
				VoVehAppDevice head = new VoVehAppDevice();
				head.setAddress("安装地址");
				head.setCreateTime("安装时间");
				head.setDeviceId("机具编号");
				head.setNote("备注类型");
				head.setVehCode("自编号");
				head.setNoteDetails("备注内容");
				head.setTelephone("安装人手机号码");
				head.setVehNo("车牌号");
				map.put("head", head);
				map.put("page", page);
				map.put("line", voVehAppDeviceList);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("暂无数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	// 根据id得到VehAppDevice
	@RequestMapping(value = "/getbyIdVehAppDevice")
	@ResponseBody
	public Map<String, Object> getbyIdVehAppDevice(int id) {
		Map<String, Object> map = new HashMap<>();
		try {
			VehAppDevice vehAppDevice = vehAppDeviceService.getById(id);
			if(vehAppDevice != null){
				VoVehAppDevice voVehAppDevice = new VoVehAppDevice();
				voVehAppDevice.setId(vehAppDevice.getId());
				voVehAppDevice.setTelephone(vehAppDevice.getTelephone());
				voVehAppDevice.setCreateTime(UtilDate.getDateFormatter(vehAppDevice.getCreateTime()));
				voVehAppDevice.setAddress(vehAppDevice.getAddress());
				voVehAppDevice.setDeviceId(vehAppDevice.getDeviceId());
				voVehAppDevice.setVehCode(vehAppDevice.getVehCode());
				Integer note = vehAppDevice.getNote();
				String ischan = note + "";
				String notes = null;
				if (ischan.equals("1")) {
					notes = "换机具";
				} else if (ischan.equals("2")) {
					notes = "换流量卡";
				} else if (ischan.equals("3")) {
					notes = "其他";
				} else {
					notes = "暂无数据";
				}
				voVehAppDevice.setNote(notes);
				voVehAppDevice.setNoteDetails(vehAppDevice.getNoteDetails());
				voVehAppDevice.setVehNo(vehAppDevice.getVehNo());
				map.put("line", voVehAppDevice);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("查询失败");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	// 修改已安装车辆备注信息
	@RequestMapping(value = "/updateVehAppDevice")
	@ResponseBody
	public Map<String, Object> updateVehAppDevice(int id, String note, String noteDetails) {
		Map<String, Object> map = new HashMap<>();
		try {
			VehAppDevice vehAppDevice = vehAppDeviceService.getById(id);
			if(vehAppDevice != null){
				if(StringUtils.isNotBlank(note)){
					note = URLDecoder.decode(note,"UTF-8");
					int notes = 0;
					if(note.equals("1")){
						notes = 1;
					}else if(note.equals("2")){
						notes = 2;
					}else if(note.equals("3")){
						notes = 3;
					}
					vehAppDevice.setNote(notes);
				}
				if(StringUtils.isNotBlank(noteDetails)){
					noteDetails = URLDecoder.decode(noteDetails,"UTF-8");
					vehAppDevice.setNoteDetails(noteDetails);
				}
				vehAppDeviceService.updateVehAppDevice(vehAppDevice);
				voStatus.setCode(1);
				voStatus.setMessage("修改成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(1);
			voStatus.setMessage("修改失败");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}
	//录入APP录入历史查询
	@RequestMapping(value = "/appHistory")
	@ResponseBody
	public Map<String, Object> appUpdata(String telephone, String deviceId, String vehNo,
			String appKey, String pageIndex,String vehCode) {
		Map<String, Object> map = new HashMap<>();
		String pageSize = "10";// 每一页的行数
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "1";
		}
		Page page = new Page();
		page.setPageIndex(Integer.parseInt(pageIndex));
		page.setPageSize(Integer.parseInt(pageSize));

		try {
			if (StringUtils.isNotBlank(vehNo)) {
				vehNo = URLDecoder.decode(vehNo, "UTF-8");
			}


			Map<String, Object> conditions = new HashMap<>();
			conditions.put("vehCode", vehCode);
			conditions.put("telephone", telephone);
			conditions.put("deviceId", deviceId);
			conditions.put("vehNo", vehNo);
			conditions.put("appKey", appKey);

			int count = vehAppOldDeviceService.getFindVehAppOldDeviceCount(conditions);
			page = ToolUtil.getPage(count, page);

			conditions.put("pageSize", page.getPageSize());
			conditions.put("startRow", page.startRow());

			List<VehAppOldDevice> list = vehAppOldDeviceService.getFindVehAppOldDevice(conditions);
			if (list != null && list.size() != 0) {
				List<VoVehAppDevice> voVehAppDeviceList = new ArrayList<>();
				for (VehAppOldDevice vehAppOldDevice : list) {
					VoVehAppDevice voVehAppDevice = new VoVehAppDevice();
					voVehAppDevice.setTelephone(vehAppOldDevice.getTelephone());
					voVehAppDevice.setId(vehAppOldDevice.getId());
					voVehAppDevice.setAddress(vehAppOldDevice.getAddress());
					voVehAppDevice.setDeviceId(vehAppOldDevice.getDeviceId());
					voVehAppDevice.setVehNo(vehAppOldDevice.getVehNo());
					voVehAppDevice.setVehCode(vehAppOldDevice.getVehCode());
					voVehAppDevice.setCreateTime(UtilDate.getDateFormatter(vehAppOldDevice.getCreateTime()));
					voVehAppDeviceList.add(voVehAppDevice);
				}
				VoVehAppDevice head = new VoVehAppDevice();
				head.setAddress("安装地址");
				head.setCreateTime("安装时间");
				head.setDeviceId("机具编号");
				head.setNote("备注类型");
				head.setVehCode("自编号");
				head.setNoteDetails("备注内容");
				head.setTelephone("安装人手机号码");
				head.setVehNo("车牌号");
				map.put("head", head);
				map.put("page", page);
				map.put("line", voVehAppDeviceList);
				voStatus.setCode(1);
				voStatus.setMessage("查询成功");
				map.put("status", voStatus);
				return map;
			}
			voStatus.setCode(0);
			voStatus.setMessage("暂无数据");
			map.put("status", voStatus);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}
}
